//
//  NSMutableString+Safe.m
//  CommonTool
//
//  Created by Name i on 2022/4/14.
//

#import "NSMutableString+Safe.h"

@implementation NSMutableString (Safe)



+ (void)exchangeMethod {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class stringClass = NSClassFromString(@"__NSCFString");
        //replaceCharactersInRange
        [NSObject tnSwizzlingInstanceMethod:@selector(replaceCharactersInRange:withString:)
                                 withMethod:@selector(safe_replaceCharactersInRange:withString:)
                                  withClass:stringClass];
        //insertString:atIndex:
        [NSObject tnSwizzlingInstanceMethod:@selector(insertString:atIndex:)
                                 withMethod:@selector(safe_insertString:atIndex:)
                                  withClass:stringClass];
        //deleteCharactersInRange
        [NSObject tnSwizzlingInstanceMethod:@selector(deleteCharactersInRange:)
                                 withMethod:@selector(safe_deleteCharactersInRange:)
                                  withClass:stringClass];
    });
}


- (void)safe_replaceCharactersInRange:(NSRange)range withString:(NSString *)aString {
    @try {
        [self safe_replaceCharactersInRange:range withString:aString];
    }
    @catch (NSException *exception) {
        [NNDefender crashWithException:exception defaultToDo:    NNDefenderDefaultIgnore];
    }
    @finally {
        
    }
}

- (void)safe_insertString:(NSString *)aString atIndex:(NSUInteger)loc {
    @try {
        [self safe_insertString:aString atIndex:loc];
    }
    @catch (NSException *exception) {
        [NNDefender crashWithException:exception defaultToDo:    NNDefenderDefaultIgnore];
    }
    @finally {
        
    }
}

- (void)safe_deleteCharactersInRange:(NSRange)range {
    @try {
        [self safe_deleteCharactersInRange:range];
    }
    @catch (NSException *exception) {
        [NNDefender crashWithException:exception defaultToDo:    NNDefenderDefaultIgnore];
    }
    @finally {
        
    }
}






@end
